package com.zq.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.zq.comm.Const;
import com.zq.pojo.Dept;
import com.zq.service.DeptService;
@Controller("deptAction")
public class DeptAction implements ModelDriven, RequestAware{
	//在这里定义一个变量叫pageNo，在提供他的方法，就能或得到前台发过来的页数
	private int pageNo;
	
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	
	private Map<String,Object> requestMap;
	@Override
	public void setRequest(Map<String, Object> requestMap) {
		// TODO Auto-generated method stub
		this.requestMap = requestMap;
	}

	private Dept dept = new Dept();
	@Override
	public Dept getModel() {
		
		return dept;
	}
	@Autowired
	private DeptService deptService;
	
	
	
	private String option;

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}
	//增
	public String save()
	{
		
		return deptService.add(dept);
	}
	//删
	public String delete()
	{
		Dept dept1 = deptService.findByDeptno(dept.getDeptNo());
		return deptService.delete(dept1);
	} 
	//改
	public String update()
	{
		
		return deptService.update(dept);
	}
	public String findById()
	{
		String msg = Const.ERROR;
		Dept dept1 = deptService.findByDeptno(dept.getDeptNo());
		if(dept1 != null)
		{
			if (Const.UPDATE.equalsIgnoreCase(option)) {
				requestMap.put("dept", dept1);
				msg = Const.UPDATE;// update -->dispatcher WEB-INF/views/emp_update.jsp
			}else{
			List<Dept> deptList = new ArrayList<>();
			deptList.add(dept1);
			requestMap.put("deptList", deptList);
			msg = Const.SUCCESS;
			}
		}
		
		return msg;
	}
	public String findAll()
	{		
		 pageNo = 1;
		 
		String msg = Const.ERROR;
			List<Dept> deptList = deptService.findAll(pageNo);
			if (deptList != null && deptList.size() > 0) {
				requestMap.put("deptList", deptList);
				msg = Const.SUCCESS;
		}
		return msg;
	}
	//分页
	
	public String page()
	{	
		
		
		
		String msg = Const.ERROR;
		
		List<Dept> depts = deptService.getPage(pageNo).getList();
		if(depts!= null && depts.size() > 0)
		{
			requestMap.put("deptList", depts);
			msg = Const.SUCCESS; 
		}
		return msg;
	}
}
